#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "mpx_supt.c"
 #include <dos.h>
 #include <dir.h>
 #include <time.h>
 
 // for display directories
 #define MAX_PATH_SIZE 50
 #define ERR_DIR_OPEN 10
 #define ERR_NO_FILE 20
 
 #define SUCCESS 0
 
 
 /* static char array to store the current directory */
 static char curr_path[MAX_PATH_SIZE];
 
 /* Entry of ffblk */
 static struct ffblk file_block;
 
 /* */
 static int num_entries;
 static struct date sys_date; /* MPX system date */
 
 
 //Version information
 float vers = 1.0;
 int modMonth = 9;
 int modDay = 19;
 int modYear = 2008;
 
 
 //global variables for readcmd (), processcmd()
 static int x=1; //x is our int length of command string, to be used later, y is used to keep track of number of paramaters
 static char *scmd = {NULL};
 char *par1, *par2, *par3; //Parameter 1, 2, 3
 
 
 // declaration of functions
 void readcmd();
 //void clearcmd();
 void processcmd();
 void mpx_exit();
 void version();
 void disp( );
 void help(char func[]);
 void welcome();
 void date(char opt[6], char dte[20]);
 
 
 int main(int argc, char **argv)
 {
 
 welcome();
 getdate(&sys_date);
 //added: use an endless cycle to read, process commands.
 //added: match the input commands with our commands libray and
 //added: decide which functions should be loaded.
 
 while (1)
 {
 readcmd();
 processcmd();
 
 
 if (!strcmp(par1,"help"))
 {
 printf("Load help function.\n");
 help(par2);
 }
 else if ( !strcmp(par1,"version"))
 {
 printf("Load version function.\n");
 version();
 }
 else if ( !strcmp(par1,"date"))
 {
 printf("Load date function.\n");
 date(par2,par3);
 }
 else if ( !strcmp(par1,"display") )
 {
 printf("Load display function.\n");
 disp();
 
 }
 else if ( !strcmp(par1,"exit") )
 {
 printf("Load exit function.\n");
 mpx_exit();
 
 }
 else
 {
 printf("Unrecognized command. Try again.\n");
 }
 }
 
 }
 
 
 void welcome ()
 {
 printf ("Welcome to MPX!\n");
 printf("Please make a valid selection using the following commands. prompt.\n");
 
 printf("1 Set/Get Date\n");
 printf("2 Display\n");
 printf("3 Version\n");
 printf("4 Help\n");
 printf("5 Exit\n");
 }
 
 
 void readcmd()
 {
 printf("\n\nMPX>");
 gets(&scmd);
 }
 
 // The process function takes the string scmd, the command given by the user
 // and passes it to the appropriate function with the proper paramaters
 
 void processcmd()
 { int y=0;
 char *p;
 
 p = strtok(&scmd," ");
 
 par1=p;
 while (p != NULL)
 {
 p = strtok (NULL, " ,");
 if (y==0)
 par2=p;
 if (y==1)
 par3=p;
 y++;
 }
 
 }
 
 
 void mpx_exit()
 {
 
 char ex;
 printf("ARE YOU SURE YOU WANT TO EXIT? ");
 scanf("%c", &ex);
 
 //printf("the input is %c\n",ex);
 if(ex == 'y' || ex == 'Y')
 {
 printf("Now exiting. . . \n");
 exit(1);
 }
 else if(ex == 'n' || ex == 'N')
 {
 return;
 }
 else
 {
 printf("Invalid command.\n");
 return;
 }
 }
 
 
 void version()
 {
 printf("Version Number: ");
 printf("%f\n",vers);
 printf("Last modified: ");
 printf("%d / %d / %d\n", modMonth, modDay, modYear);
 }
 
 
 
 void disp( )
 { char namebuf[60];
 long *filesize;
 //printf("the directory parameter is %s", par2);
 if (sys_open_dir (par2))
 printf("Can't open the file.\n");
 
 while(!sys_get_entry ( namebuf, 60, filesize))
 {
 printf ("%40s\t", namebuf);
 printf ("%10ld\n", *filesize);
 }
 
 if (sys_close_dir())
 printf("Can't close the directory.\n");
 
 }
 
 
 void help(char* func){
 
 FILE *file;
 char c;
 char* fn;
 
 //printf("func= %s\n", func);
 fn = (char*)(sys_alloc_mem(25*sizeof(fn)));
 strcat(fn,"HELP\\");
 
 printf("Entering help...\n \n");
 
 if(func == NULL)
 {
 file=fopen("HELP\\COMMAND.TXT","r");
 if(file==NULL){
 printf("Cannot open command list. Sorry :( \n \n");
 return;
 }
 else{
 while(!feof(file))
 {
 c = fgetc(file);
 fputc(c,stdout);
 } 
 
 }
 
 printf("\n \n");
 
 }
 
 else
 {
 strcat(fn,func);
 strcat(fn,".HLP");
 
 //printf("the help file is %s\n", fn);
 
 file=fopen(fn,"r");
 if(file==NULL)
 {
 printf("Cannot open specified function definition.Sorry. \n \n");
 return;
 }
 else
 {
 while(!feof(file))
 {
 c = fgetc(file);
 fputc(c,stdout);
 } 
 
 }
 printf("\n \n");
 
 }
 fclose(file);
 } //end help
 
 
 void date(char opt[6], char dte[20]){
 char *p;
 int mon, day, yr;
 int i=0, temp;
 date_rec aDate;
 
 if(strlen(opt)==3){
 sys_get_date(&aDate);
 
 mon=aDate.month;
 day=aDate.day;
 yr=aDate.year;
 
 if(strcmp("set",opt)==0){
 p=strtok(dte,"-");
 while(p!=NULL){
 temp=atoi(p);
 if(i==0){
 if(temp>=1 && temp<=12)
 aDate.month=temp;
 else{
 printf("\nNOT A VALID DATE!\n");
 return;
 }
 }
 if(i==1){
 if((aDate.month==9 || aDate.month==4 || aDate.month==6 || aDate.month==11) && temp<=30){
 aDate.day=temp;
 
 }
 else if(aDate.month==2 && temp<=29){
 aDate.day=temp;
 
 }
 else if((aDate.month==1 || aDate.month==3 ||aDate.month==5 || aDate.month==7 || aDate.month==8 || aDate.month==10 || aDate.month==12) && temp<=31){
 aDate.day=temp;
 
 }
 else{
 printf("\nNOT A VALID DATE!\n");
 aDate.month=mon;
 aDate.day=day;
 aDate.year=yr;
 return;
 }
 }
 if(i==2){
 if(aDate.month==2 && aDate.day==29){
 if(temp%4==0){
 aDate.year=temp;
 printf("Date successfully set!");
 return;
 }
 else{
 printf("\nNOT A VALID DATE!");
 aDate.month=mon;
 aDate.day=day;
 aDate.year=yr;
 return;
 }
 }
 else{
 aDate.year=temp;
 printf("Date successfully set!");
 }
 }
 i++;
 p=strtok(NULL, "-");
 }
 //printf("%d %d %s\n", aDate.month, aDate.day, aDate.year);
 
 sys_set_date(&aDate);
 
 }
 
 else if(strcmp("get",opt)==0){
 sys_get_date(&aDate);
 
 switch (aDate.month){
 case 1:
 printf("January %d, %u \n", aDate.day, aDate.year);
 break;
 case 2:
 printf("February %d, %d \n", aDate.day, aDate.year);
 break;
 case 3:
 printf("March %d, %d \n", aDate.day, aDate.year);
 break;
 case 4:
 printf("April %d, %d \n", aDate.day, aDate.year);
 break;
 case 5:
 printf("May %d, %d \n", aDate.day, aDate.year);
 break;
 case 6:
 printf("June %d, %d \n", aDate.day, aDate.year);
 break;
 case 7:
 printf("July %d, %d \n", aDate.day, aDate.year);
 break;
 case 8:
 printf("August %d, %d \n", aDate.day, aDate.year);
 break;
 case 9:
 printf("September %d, %d \n", aDate.day, aDate.year);
 break;
 case 10:
 printf("October %d, %d \n", aDate.day, aDate.year);
 break;
 case 11:
 printf("November %d, %d \n", aDate.day, aDate.year);
 break;
 case 12:
 printf("December %d, %d \n", aDate.day, aDate.year);
 break;
 case NULL:
 printf("Date has not yet been set. Please set today's date.\n");
 break;
 default:
 printf("Invalid date. Something went wrong here. . .\n");
 printf("Does this help? Month: %d, Day: %d, Year: %d\n", aDate.month, aDate.day, aDate.year);
 break;
 }
 }
 
 else{
 printf("Not a recognized command. . .");
 }
 
 }
 else
 printf("Not a recognized command. . .");
 }
 